package net.oschina.app.adapter.explore;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.base.BaseListAdapter;
import net.oschina.app.bean2.ActiveBean;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by 张运湘 on 2017/1/13.
 */

public class FriendsRircleAdapter extends BaseListAdapter<ActiveBean> {


    private Context mContext;

    public FriendsRircleAdapter(Context context) {
        mContext = context;
    }
    private setItemOnclickListener mItemOnclickListener=null;

    public void setItemOnclickListener(setItemOnclickListener itemOnclickListener) {
        mItemOnclickListener = itemOnclickListener;
    }

    public interface setItemOnclickListener{
        //跳转到详情页面
        Void toDetilactivity();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View friendsView = LayoutInflater.from(mContext).inflate(R.layout.activity_find_friendscircle_item, parent, false);
        RecyclerView.ViewHolder viewHolder = new friendsViewHolder(friendsView);
        //点击监听
        friendsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemOnclickListener!=null){
                    mItemOnclickListener.toDetilactivity();
                }
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ActiveBean activeBean = mList.get(position);
        //昵称
        ((friendsViewHolder) holder).mTvFriendsRricleItemName.setText(activeBean.getAuthor());
        ((friendsViewHolder) holder).mTvFriendsRricleItemName.setTextColor(Color.BLACK);
        //时间
        ((friendsViewHolder) holder).mTvFriendsRricleItemTime.setText(activeBean.getPubDate());
        //评论
        if (activeBean.getCommentCount() != 0) {
            ((friendsViewHolder) holder).mTvFriendsRricleItemMsgNumber.setText(String.valueOf(activeBean.getCommentCount()));
            ((friendsViewHolder) holder).mTvFriendsRricleItemMsgNumber.setVisibility(View.VISIBLE);
        } else {
            ((friendsViewHolder) holder).mTvFriendsRricleItemMsgNumber.setVisibility(View.GONE);
        }
        //客服端手机类型
        switch (activeBean.getAppclient()) {
            case 3:
                ((friendsViewHolder) holder).mTvFriendsRricleItemAppclient.setText("Android");
                ((friendsViewHolder) holder).mTvFriendsRricleItemAppclient.setVisibility(View.VISIBLE);
                break;
            case 4:
                ((friendsViewHolder) holder).mTvFriendsRricleItemAppclient.setText("IPhone");
                ((friendsViewHolder) holder).mTvFriendsRricleItemAppclient.setVisibility(View.VISIBLE);
                break;
            default:
                ((friendsViewHolder) holder).mTvFriendsRricleItemAppclient.setVisibility(View.GONE);
                break;
        }
        //动态类型,标题
        switch (activeBean.getCatalog()) {
            case 0:
                if (activeBean.getObjecttitle() != null) {
                    SpannableString str = new SpannableString("分享了一段代码:" + activeBean.getObjecttitle());
                    str.setSpan(new ForegroundColorSpan(Color.BLUE), 8, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 8, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText(str);

                } else {
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText("分享了一段代码");
                }
                break;
            case 1:
                if (activeBean.getObjecttitle() != null) {
                    SpannableString str = new SpannableString("什么鬼:" + activeBean.getObjecttitle());
                    str.setSpan(new ForegroundColorSpan(Color.BLUE), 4, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 4, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText(str);
                } else {
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText("什么鬼");
                }
                break;
            case 2:
                if (activeBean.getObjecttitle() != null) {
                    SpannableString str = new SpannableString("回答了问题:" + activeBean.getObjecttitle());
                    str.setSpan(new ForegroundColorSpan(Color.BLUE), 6, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 6, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText(str);
                } else {
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText("回答了问题");
                }
                break;
            case 3:
                if (activeBean.getObjecttitle() != null) {
                    SpannableString str = new SpannableString("更新了动态:" + activeBean.getObjecttitle());
                    str.setSpan(new ForegroundColorSpan(Color.BLUE), 6, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 6, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText(str);
                } else {
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText("更新了动态");
                }
                break;
            case 4:
                if (activeBean.getObjecttitle() != null) {
                    SpannableString str = new SpannableString("发表了博客:" + activeBean.getObjecttitle());
                    str.setSpan(new ForegroundColorSpan(Color.BLUE), 6, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 6, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText(str);

                } else {
                    ((friendsViewHolder) holder).mTvFriendsCircleTitle.setText("发表了博客");
                }
                break;
        }
        //内容展示
        ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setText(activeBean.getMessage());
        if (TextUtils.isEmpty(activeBean.getMessage())){
            ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setVisibility(View.GONE);
        }else {
            ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setFocusable(true);
//            ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setDispatchToParent();
            ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setMovementMethod(LinkMovementMethod.getInstance());
            ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setLongClickable(true);

            /*           <html>
            <head></head>
            <body>
             吃个饭，开始忙活，还可以边看
             <a href="http://m.oschina.net/tweet-topic/%E3%80%90%E6%9D%AD%E5%B7%9E%E3%80%91%E6%BA%90%E5%88%9B%E4%BC%9A%E7%AC%AC37%E6%9C%9F%E5%BC%80%E5%A7%8B%E6%8A%A5%E5%90%8D">
             #【杭州】源创会第37期开始报名#</a>，[大兵]
            </body>
           </html>*/
            Spanned spanned = Html.fromHtml(activeBean.getMessage());
            ((friendsViewHolder) holder).mTvFriendsRricleItemContent.setText(spanned);
        }

    }

    class friendsViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_friendsRricleItem_headicon)
        ImageView mIvFriendsRricleItemHeadicon;
        @BindView(R.id.tv_friendsRricleItem_name)
        TextView mTvFriendsRricleItemName;
        @BindView(R.id.tv_friendsRricleItem_time)
        TextView mTvFriendsRricleItemTime;
        @BindView(R.id.tv_friendsRricleItem_content)
        TextView mTvFriendsRricleItemContent;
        @BindView(R.id.tv_friendsRricleItem_msgNumber)
        TextView mTvFriendsRricleItemMsgNumber;
        @BindView(R.id.tv_friendsCircle_title)
        TextView mTvFriendsCircleTitle;
        @BindView(R.id.tv_friendsRricleItem_appclient)
        TextView mTvFriendsRricleItemAppclient;
        public friendsViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
